<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        // working with url, form
        $this->load->helper(array('url', 'form'));
        
        // initial model User
        $this->load->model('User_model');

    }

    // member_area section
    public function member_area()
    {
        $this->load->library('session');
        $this->load->view('frontend/member_area');
    }
    
    // destroy session
    public function logout()
    {
        $this->load->library('session');
        $this->load->helper('cookie');
        $this->session->sess_destroy(); // destroy session
        $this->input->set_cookie('user', '', time() - 3600);
        
        // return home page!
        redirect(base_url()."index.php/main_layout");
        exit();
    }

    public function login()
    {
        // working form validation
        $this->load->library('form_validation');

        // working with session
        $this->load->library('session');
        
        // cookie
        $this->load->helper("cookie"); // using cookie                        
                      
        // set rules for login form
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');

        // check data validation
        if($this->form_validation->run() == FALSE) {
            $this->load->view('frontend/login');
        }
        else
        {
            if(isset($_POST['submit'])) 
            {
                $username = $this->input->post('username');
                $password = sha1($this->input->post('password'));
                
                $remember = $this->input->post('remember'); // choose remember me!
                                
                $flag = $this->User_model->check_acc_login($username, $password);
                
                if($flag) {
                    
                    if($remember == 1)
                    {                                                                                                                    
                        // set_cookie()
                        $this->input->set_cookie('user', $username, time() + 1800); 
                        
                        $this->load->view('frontend/member_area');
                    }
                    else {
                        // config session data
                        $data['account'] = array(
                            'username' => $username,
                            'password' => $password,
                        ); 
                        $this->session->set_userdata($data); 
                        $this->load->view('frontend/member_area', $data);                      
                    }
                                        
                    
                }
                else {
                    echo "Login fail";
                }
            }            
        }
        
    }
}